package net.jeeshop.core.sms;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collections;

import net.jeeshop.services.manage.sms.bean.Sms;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


/**
 * http://www.webchinese.com.cn   此公司的SMS短信平台
 * @author jqsl2012@163.com
 *
 */
public class SMSWebChinese {
	public static void main(String[] args) throws HttpException, IOException {
		sendSMS(null);
	}

	public static void sendSMS(Sms sms) throws IOException, HttpException,
			UnsupportedEncodingException {
		HttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost("http://utf8.sms.webchinese.cn");
		post.setHeader("Content-Type",
                "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
		NameValuePair[] data = { new BasicNameValuePair("Uid", "jqsl2012"),
				new BasicNameValuePair("Key", "8376220944aae870d31a"),
				new BasicNameValuePair("smsMob", sms.getPhone()),
				new BasicNameValuePair("smsText", sms.getContent()) };
		post.setEntity(new UrlEncodedFormEntity(Arrays.asList(data), Consts.UTF_8));

        HttpResponse response = client.execute(post);
		Header[] headers = response.getAllHeaders();
		int statusCode = response.getStatusLine().getStatusCode();
		System.out.println("statusCode:" + statusCode);
		for (Header h : headers) {
			System.out.println("h.toString()="+h.toString());
		}
		String returnCode = EntityUtils.toString(response.getEntity(),"gbk");
		System.out.println("result="+returnCode);

		post.releaseConnection();
		
		sms.setReturnCode(returnCode);
	}
}
